package it.robbocca.backup.smb;

import it.robbocca.backup.wifi.parameter.BackupParameter;
import it.robbocca.backup.wifi.parameter.BackupType;
import it.robbocca.backup.wifi.preferences.SettingsActivity;

import java.io.File;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.Environment;
import android.preference.EditTextPreference;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class WifiBackup extends Activity {
	WifiBackup win= null;
	private String PREFS_NAME="config_key_sync_method";
	private BackupParameter parameters=new BackupParameter();
	
	private RadioGroup rGroupBackUpType=null;
	private RadioButton rButtonBackUuType=null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_backup);
        
        Button button = (Button)findViewById(R.id.button1);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(showHostListener);
        
        Button button2 = (Button)findViewById(R.id.button2);
        // Register the onClick listener with the implementation above
        button2.setOnClickListener(backupListener);
        
        Button button3 = (Button)findViewById(R.id.button3);
        // Register the onClick listener with the implementation above
        button3.setOnClickListener(browseFtpListener);
        
        updateSettings();
        
        updateGui();
        
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
      sharedPref.registerOnSharedPreferenceChangeListener(new PrefernceChangeListener(this));
     // Restore preferences
//        PREFS_NAME= R.string.config_key_sync_method
       

        
        
//        
//        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//        sharedPref.registerOnSharedPreferenceChangeListener(new PrefernceChangeListener(this));
//        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
//        String value = settings.getString("folder", "");
//        parameters.setFolder(value);
//        value = settings.getString("server", "");
//        parameters.setServer(value);
//        value = settings.getString("backType", "");
//        
//        parameters.setBackupType((value.equals("")?BackupType.FTP:BackupType.valueOf(value)));
//        
//        rGroupBackUpType=(RadioGroup) findViewById(R.id.rGroupBackupType);
//       
//        rButtonBackUuType = (RadioButton) findViewById(R.id.rbFtp);
//        rButtonBackUuType.setSelected(true);
        
        win=this;
    }

   
    
    
    private void updateGui() {
    	((TextView)findViewById(R.id.textView2)).setText(" "+parameters.getServer());
		setTitle("WifiBackup" + " - method ("+parameters.getBackupType()+")");
	}




	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_wifi_backup, menu);
        return true;
    }
    
    
    
    
    
    
    private OnClickListener browseFtpListener = new OnClickListener() {
    	
		public void onClick(View v) {
			Intent myIntent = new Intent(WifiBackup.this, SelectFolderActivity.class);
			
//			myIntent.putExtra("url","smb://192.168.0.1/");
			myIntent.putExtra("host","192.168.0.1");
			myIntent.putExtra("folder","/shares/USB_Storage/");
			myIntent.putExtra("mode","FTP");
			
			WifiBackup.this.startActivity(myIntent);
			// passo parametri
		}
    };
    
    private OnClickListener showHostListener = new OnClickListener() {
    	
		public void onClick(View v) {
			Intent myIntent = new Intent(WifiBackup.this, SelectFolderActivity.class);
//			myIntent.putExtra("url","smb://192.168.0.1/");
			myIntent.putExtra("host","192.168.0.5/");
			myIntent.putExtra("folder","");
			myIntent.putExtra("mode","SMB");
			WifiBackup.this.startActivity(myIntent);
			
			// passo parametri
		}
	};
	
	private OnClickListener backupListener = new OnClickListener() {
    	String smbServer="smb://192.168.0.5";
    	
    	public void onClick(View v) {
    		
    		

    		
    		
			System.out.println("asas");
			boolean mExternalStorageAvailable = false;
			boolean mExternalStorageWriteable = false;
			String state = Environment.getExternalStorageState();

			if (Environment.MEDIA_MOUNTED.equals(state)) {
			    // We can read and write the media
			    mExternalStorageAvailable = mExternalStorageWriteable = true;
			} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			    // We can only read the media
			    mExternalStorageAvailable = true;
			    mExternalStorageWriteable = false;
			} else {
			    // Something else is wrong. It may be one of many other states, but all we need
			    //  to know is we can neither read nor write
			    mExternalStorageAvailable = mExternalStorageWriteable = false;
			}
			
			if (mExternalStorageAvailable==true){
				File f=Environment.getExternalStorageDirectory();
				String[] files=f.list();
				for(int i=0;i<files.length;i++){
					System.out.println(files[i]);
				}
				
			}
			
		}

		
	};


	




	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		 // One of the group items (using the onClick attribute) was clicked
        // The item parameter passed here indicates which item it is
        // All other menu item clicks are handled by onOptionsItemSelected()
    	
    	Intent myIntent = new Intent(WifiBackup.this, SettingsActivity.class);
    	
    	WifiBackup.this.startActivity(myIntent);
    	
		return super.onMenuItemSelected(featureId, item);
	}




	@Override
	protected void onResume() {
		 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		 Map<String, ?> map = sharedPref.getAll();
		 Object o = map.get("syncServer");
		 TextView tv=(TextView)findViewById(R.id.textView2);
		super.onResume();
	}


	private void updateSettings(){
		if (parameters==null) parameters=new BackupParameter();
		
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		String key=getString(R.string.config_key_sync_server);
    	String value = sharedPref.getString(key,"0.0.0.0");
	    parameters.setServer(value);

		key=getString(R.string.config_key_auto_sync);
    	Boolean valueB = sharedPref.getBoolean(key,false);
    	parameters.setAutoSync(valueB);

		key=getString(R.string.config_key_backup_all);
		valueB = sharedPref.getBoolean(key,true);
    	parameters.setBackAll(valueB);

		key=getString(R.string.config_key_backup_img);
		valueB = sharedPref.getBoolean(key,false);
    	parameters.setBackimg(valueB);

		key=getString(R.string.config_key_backup_music);
		valueB = sharedPref.getBoolean(key,false);
    	parameters.setBackmusic(valueB);

		key=getString(R.string.config_key_backup_video);
		valueB = sharedPref.getBoolean(key,false);
    	parameters.setBackvideo(valueB);

		key=getString(R.string.config_key_sync_method);
    	value = sharedPref.getString(key,BackupType.SMB.name());
    	parameters.setBackupType(BackupType.valueOf(value));

    	key=getString(R.string.config_key_backup_folder);
    	value = sharedPref.getString(key,"");
    	parameters.setFolder(value);
	}
	
	
} 

class PrefernceChangeListener implements OnSharedPreferenceChangeListener{
	private Activity v=null;
	
	PrefernceChangeListener(Activity a){
		v=a;
	}
	
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		Log.d("WifiBackup", key);
		
    	if (key.trim().equals(v.getString(R.string.config_key_sync_server))){
    		String aa = sharedPreferences.getString(key,"0.0.0.0");
	    	System.out.println(aa);	
//	    	((EditTextPreference)sharedPreferences).setSummary(aa);
	    	((TextView)v.findViewById(R.id.textView2)).setText(aa);
    	}
    	
    	
    	
//        if (key.equals(KEY_PREF_SYNC_CONN)) {
//            Preference connectionPref = findPreference(key);
//            // Set summary to be the user-description for the selected value
//            connectionPref.setSummary(sharedPreferences.getString(key, ""));
//        }
		
	}
	
	
}